package com.xingkeduo.business.user.controller;

import com.xingkeduo.business.transformation.json.*;
import com.xingkeduo.business.user.domain.AssessmentDomain;
import com.xingkeduo.business.user.domain.ManageInfoDomain;
import com.xingkeduo.business.user.event.EventHandleService;
import com.xingkeduo.business.user.event.MsgTempDTO;
import com.xingkeduo.business.user.service.AuthorizationService;
import com.xingkeduo.pojo.dto.SubscribeDTO;
import com.xingkeduo.pojo.dto.WxMaUserInfo;
import com.xingkeduo.pojo.enums.SendingTypeEnum;
import com.xingkeduo.pojo.table.JobseekerJobDetailTO;
import com.xingkeduo.pojo.table.JobseekerJobTO;
import com.xingkeduo.pojo.table.JobseekerSocialTO;
import com.xingkeduo.pojo.utils.ResponseUtils;
import com.xingkeduo.util.pojo.base.BaseController;
import com.xingkeduo.util.tool.ReturnUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 统一授权
 *
 * @Description:
 * @Author:gaozifeng
 * @Date 2019/5/2 17:32
 **/
@RestController
@RequestMapping("/authorization")
public class WxAuthorizationController extends BaseController {

    private final Logger log = LoggerFactory.getLogger(this.getClass());

    @Resource
    private ManageInfoDomain manageInfoDomain;
    @Resource
    private AssessmentDomain assessmentDomain;
    @Resource
    private AuthorizationService authorizationService;
    @Resource
    private EventHandleService eventHandleService;

    /**
     * 1、拉取求职者信息,存储授权表
     *
     * @param :
     * @return : java.lang.String
     * @author : gaozifeng
     * @date : 2019/4/19 15:43
     */
    @PostMapping("/save")
    public String saveJobseeker(@RequestBody SubscribeDTO dto) {
        log.info("controllerDto---------------:" + dto.toString());
        returnUtil = new ReturnUtil();
        if (null == dto.getMpId() && null == dto.getCode()) {
            return returnUtil.getException("0", "数据不能为空");
        }
        try {
            return returnUtil.getSuccess(JobseekerWechatJTO.toJson(authorizationService.saveJobseeker(dto)));
        } catch (Throwable t) {
            return dealException(t, returnUtil);
        }
    }

    /**
     * 2、求职者创建账号
     *
     * @param body:
     * @return : java.lang.String
     * @author : gaozifeng
     * @date : 2019/5/3 16:15
     */
    @PostMapping(value = "/enter")
    public String enterDoPost(@RequestBody String body) {
        returnUtil = new ReturnUtil();
        try {
            log.info("body.login---------------------" + body.toString());
            return returnUtil
                    .getSuccess(JobseekerJTO.toJson(assessmentDomain.regist(JobseekerJTO.createTableForNew(body))));
        } catch (Throwable t) {
            return dealException(t, returnUtil);
        }
    }

    /**
     * 注册第二步-入职填写个人信息接口
     *
     * @param body
     * @return
     * @author 高杰
     * @time 2018年12月29日 上午11:51:18
     */
    @PostMapping(value = "/enter/social")
    public String doPost(@RequestBody String body) {
        returnUtil = new ReturnUtil();
        try {
            assessmentDomain.fillJobseekerSocial(JobseekerSocialJTO.createTableForNew(body));
            return returnUtil.getSuccess();
        } catch (Throwable t) {
            return dealException(t, returnUtil);
        }
    }

    /**
     * 注册第三步 入职填写工作经历接口
     *
     * @param body
     * @return
     * @author 高杰
     * @time 2018年12月29日 上午11:50:51
     */
    @PostMapping(value = "/enter/jobdetail")
    public String jobdetail(@RequestBody String body) {
        returnUtil = new ReturnUtil();
        try {
            log.info("authorization.jobdetail 参数信息: " + body);
            JobseekerJobTO job = JobseekerJobJTO.createTableForNewDetail(body);
            List<JobseekerJobDetailTO> jobDetails = JobseekerJobDetailJTO.createTableListForNew(body);

            assessmentDomain.fillJobseekerJobDetails(job, jobDetails);
            return returnUtil.getSuccess();
        } catch (Throwable t) {
            return dealException(t, returnUtil);
        }
    }

    /**
     * 注册第四步    入职填写工作信息接口
     *
     * @param body
     * @return
     * @author 高杰
     * @time 2018年12月29日 上午11:50:51
     */
    @PostMapping(value = "/enter/job")
    public String enterJob(@RequestBody String body) {
        returnUtil = new ReturnUtil();
        try {
            log.info("authorization.enterJob 参数信息: " + body);
            assessmentDomain.fillJobseekerJob(JobseekerJobJTO.createTableForNew(body));
            return returnUtil.getSuccess();
        } catch (Throwable t) {
            return dealException(t, returnUtil);
        }
    }

    /**
     * 获取求职者工作信息
     *
     * @return
     * @author 高杰
     * @time 2018年12月29日 上午11:52:26
     */
    @GetMapping(value = "/job/{url:.+}")
    public String job(@PathVariable String url) {
        returnUtil = new ReturnUtil();
        try {
            JobseekerJobTO job = manageInfoDomain.getJobseekerJob(JobseekerJobJTO.createQueryForPrimary(url));
            return returnUtil.getSuccess(JobseekerJobJTO.toJson(job));
        } catch (Throwable t) {
            return dealException(t, returnUtil);
        }
    }

    /**
     * 获取求职者个人信息
     *
     * @return
     * @author 高杰
     * @time 2018年12月29日 上午11:52:26
     */
    @GetMapping(value = "/social/{url:.+}")
    public String doGet(@PathVariable String url) {
        returnUtil = new ReturnUtil();
        try {
            JobseekerSocialTO jobseekerSocialTO = manageInfoDomain
                    .getJobseekerSocial(JobseekerSocialJTO.createQueryForPrimary(url));
            return returnUtil.getSuccess(JobseekerSocialJTO.toJson(jobseekerSocialTO));
        } catch (Throwable t) {
            return dealException(t, returnUtil);
        }
    }

    /**
     * 获取求职者工作信息详情
     *
     * @return
     * @author 高杰
     * @time 2018年12月29日 上午11:52:26
     */
    @GetMapping(value = "/detail/{url:.+}")
    public String detail(@PathVariable String url) {
        returnUtil = new ReturnUtil();
        try {
            JobseekerJobTO job = manageInfoDomain
                    .getJobseekerJobWithDetails(JobseekerJobJTO.createQueryForPrimary(url));
            return returnUtil.getSuccess(JobseekerJobJTO.toJson(job));
        } catch (Throwable t) {
            return dealException(t, returnUtil);
        }
    }


    /**
     * 阿里获取授权成功，请求过来的body数据
     *
     * @param :
     * @return : java.lang.String
     * @author : gaozifeng
     * @date : 2019/5/10 17:10
     */
    @GetMapping("/ali/code")
    public ResponseUtils<WxMaUserInfo> aliUserInfo(String code) {
        log.info("user-----ali-----code" + code);
        returnUtil = new ReturnUtil();
        try {
            WxMaUserInfo wxMaUserInfo = authorizationService.saveConsumerByAli(code);
            return ResponseUtils.getSuccess(wxMaUserInfo);
        } catch (Throwable t) {
            return ResponseUtils.error(t.getMessage(), "-1");
        }
    }

    /**
     * 微信取消关注
     *
     * @param dto:
     * @return : java.lang.String
     * @author : gaozifeng
     * @date : 2019/5/31 10:14
     */
    @PostMapping("/unsubscribe")
    public String unSubscribe(@RequestBody SubscribeDTO dto) {
        log.info("user------unSubscribe----" + dto.toString());
        returnUtil = new ReturnUtil();
        try {
            authorizationService.updateConsumerSubscribe(dto);
            return returnUtil.getSuccess();
        } catch (Throwable t) {
            return dealException(t, returnUtil);
        }
    }


    @GetMapping("/kefuxiaoxi")
    public String kefu(String openId, String content, MsgTempDTO.PushTypeEnums pushType) {
        MsgTempDTO msgTempDTO = new MsgTempDTO();
        msgTempDTO.setContent(pushType).setOpenId(openId);
        String res = null;
        if(null == msgTempDTO.getContent()){
        }else{
            res= eventHandleService.eventHandle(SendingTypeEnum.MSG_TEMP, msgTempDTO);
        }
        return res;
    }
}
